package com.lening.security.web;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.HashMap;

/**
 * @author 赵子豪
 * @date 2024年06月01日 15时29分21秒
 */
public class Request {
    public static <T> Mono<T> get(String uri, Class<T> clazz){
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.get().uri("http://"+uri).retrieve().bodyToMono(clazz);
    }

    public static <T> Mono<T> post(String uri, HashMap<Object,Object> map , Class<T> clazz){
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.post().uri("http://"+uri).bodyValue(map).retrieve().bodyToMono(clazz);
    }

    public static <T> Mono<T> Put(String uri, Object param, Class<T> clazz) {
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.put().uri("http://" + uri,param).body(BodyInserters.fromValue(param)).retrieve().bodyToMono(clazz);
    }

    public static <T> Mono<T> Delete(String uri, Object param, Class<T> clazz) {
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.delete().uri("http://" + uri,param).retrieve().bodyToMono(clazz);
    }
}